package com.web.ShoppingDemo.action;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.web.ShoppingDemo.model.Product;

@Controller
@Scope("prototype")
public class ProductAction extends BaseAction<Product> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/***********************后台方法********************************/
	public String queryJoinCategory(){
		pageMap = new HashMap<String, Object>();
		//关键字和分页参数查询相应的数据
		List<Product> productList = productService.queryJoinCategory(model.getName(), page, rows);
		
		//此处进行解决account的NoSession问题
		/*for(Product p:productList){
			p.getCategory().setAccount(null);
		}*/
		
		pageMap.put("rows", productList);
		//关键字查询总的页数
		pageMap.put("total", productService.getCount(model.getName()));
		return "jsonMap";
	}
	
	public void save() throws Exception{
		//得到上传文件的后缀名
		//String ext = FilenameUtils.getExtension(fileImage.getFilename());
		//实现文件上传功能,将UUID交给pic,然后进行商品的入库,完成文件的上传
		//FileUtil.copyFile(fileImage.getFile(),new File("C:/"+UUID.randomUUID().toString()+"."+ext));
		String pic = fileUpload.uploadFile(fileImage);
		model.setPic(pic);
		System.out.println(model);
		//商品入库
		productService.save(model);
	
	}
	
	public void update(){
		System.out.println(model);
		productService.update(model);
	}
	
	public String deleteByIds(){
		System.out.println("删除的ids为"+ids);
		productService.deleteByIds(ids);
		inputStream = new ByteArrayInputStream("true".getBytes());
		return "stream";
	}
	
	/***********************前台方法********************************/
	public String get(){
		request.put("product", productService.get(model.getId()));
		return "detail";
	}

}
